#!/usr/bin/python
# -*- coding: utf-8 -*-
# File: collection.py
# Author: Tomás Vírseda
# License: GPL v3
# Description: Collection operations

import urllib

from rdflib import URIRef
import gtk

from vazaarlib.utils import get_logger
from vazaarlib.namespaces import NIE

class AddResourceDialog():
    def __init__(self, gui):
        self.gui = gui
        self.log = get_logger('AddResDlg')
        self.rtype = None

        self.gui.frontend['res-addresdlg'] = self.gui.glade.widgets.get_widget("winAddResource")
        self.gui.frontend['res-ntbaddres'] = self.gui.glade.widgets.get_widget("ntbAddRes")
        self.gui.frontend['res-rbaddfile'] = self.gui.glade.widgets.get_widget("rbAddFile")
        self.gui.frontend['res-fchfile'] = self.gui.glade.widgets.get_widget("fchFile")
        self.gui.frontend['res-rbadddir'] = self.gui.glade.widgets.get_widget("rbAddDir")
        self.gui.frontend['res-fchdir'] = self.gui.glade.widgets.get_widget("fchDir")
        self.gui.frontend['res-rbaddnet'] = self.gui.glade.widgets.get_widget("rbAddNet")
        self.gui.frontend['res-etynetsite'] = self.gui.glade.widgets.get_widget("etyWebsite")
        self.gui.frontend['res-rbaddnote'] = self.gui.glade.widgets.get_widget("rbAddNote")
        self.gui.frontend['res-notetitle'] = self.gui.glade.widgets.get_widget("etyTitle")
        self.gui.frontend['res-noteext'] = self.gui.glade.widgets.get_widget("etyExt")
        self.gui.frontend['res-notedesc'] = self.gui.glade.widgets.get_widget("txtNote")
        self.gui.frontend['res-rbaddcol'] = self.gui.glade.widgets.get_widget("rbAddCol")
        self.gui.frontend['res-colname'] = self.gui.glade.widgets.get_widget("etyColName")
        self.gui.frontend['res-btnok'] = self.gui.glade.widgets.get_widget("btnAddResOk")
        self.gui.frontend['res-btncancel'] = self.gui.glade.widgets.get_widget("btnAddResCancel")
        self.gui.frontend['res-btnhelp'] = self.gui.glade.widgets.get_widget("btnAddResHelp")

        (PAGE_FILE, PAGE_DIR, PAGE_NET, PAGE_NOTE, PAGE_COL) = range(5)
        self.gui.frontend['res-rbaddfile'].connect('clicked', self.show_page, PAGE_FILE)
        self.gui.frontend['res-rbadddir'].connect('clicked', self.show_page, PAGE_DIR)
        self.gui.frontend['res-rbaddnet'].connect('clicked', self.show_page, PAGE_NET)
        self.gui.frontend['res-rbaddnote'].connect('clicked', self.show_page, PAGE_NOTE)
        self.gui.frontend['res-rbaddcol'].connect('clicked', self.show_page, PAGE_COL)
        self.gui.frontend['res-btnok'].connect('clicked', self.accept_data)
        self.gui.frontend['res-btncancel'].connect('clicked', self.hide_window)

        icon = self.gui.widget.render_icon(gtk.STOCK_ADD, gtk.ICON_SIZE_MENU)
        self.gui.frontend['res-addresdlg'].set_icon(icon)

    def show_page(self, widget, page_num):
        self.rtype = page_num
        self.gui.frontend['res-ntbaddres'].set_current_page(page_num)

    def show_window(self, operation=''):
        self.gui.frontend['res-fchfile'].show_all()
        self.gui.frontend['res-fchdir'].show_all()
        self.gui.frontend['res-addresdlg'].show_all()
        if operation == 'collection':
            self.gui.frontend['res-rbaddcol'].clicked()
        else:
            self.gui.frontend['res-rbaddfile'].clicked()

    def hide_window(self, *args):
        self.gui.frontend['res-addresdlg'].hide()
        #self.gui.manager.show_start_here()
        #self.gui.frontend['mainwin'].show_all()
        #self.gui.actionbar.starthere()

    def accept_data(self, *args):
        if (self.rtype) == 0:
            uris = self.gui.frontend['res-fchfile'].get_uris()
            for uri in uris:
                filename = urllib.unquote(uri)[7:]
                self.gui.actions.add_resource((filename,))
        elif (self.rtype) == 1:
            uris = self.gui.frontend['res-fchdir'].get_uris()
            for uri in uris:
                dirname = urllib.unquote(uri)[7:]
                self.gui.actions.add_resource((dirname,))
        elif (self.rtype) == 2:
            buffer = self.gui.frontend['res-etynetsite'].get_buffer()
            start = buffer.get_start_iter()
            end = buffer.get_end_iter()
            text = buffer.get_text(start, end)
            websites = text.split('\n')
            for website in websites:
                self.gui.actions.add_resource((website,))

        elif (self.rtype) == 3:
            buffer = self.gui.frontend['res-notedesc'].get_buffer()
            start = buffer.get_start_iter()
            end = buffer.get_end_iter()
            note = buffer.get_text(start, end)
            self.gui.actions.add_resource((note,))
        elif (self.rtype) == 4:
            colname = self.gui.frontend['res-colname'].get_text()
            self.gui.app.vstore.create_new_collection(colname)
            self.gui.colview.update_collection_view()

        self.hide_window()
